package util.cvs;

import com.opencsv.bean.CsvToBeanBuilder;
import lombok.extern.slf4j.Slf4j;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.List;

/**
 * cvs 解析工具类
 * Create on 2019-07-26 10:36
 *
 * @author tianbl
 */
@Slf4j
public class HilifeCsvToBeanBuilder<T> {
    /**
     * 要转换的Java bean
     */
    private Class<? extends T> type = null;
    /**
     * 支付对账类型
     */
    private String payType;
    /**
     * 要解析的数据 string 或者 地址
     */
    private String sourceData;

    /**
     * 带解析数据构造函数
     *
     * @param sourceData 源数据
     */
    public HilifeCsvToBeanBuilder(String sourceData) {
        this.sourceData = sourceData;
    }

    /**
     * 解析csv
     *
     * @return list<T>
     */
    public List<T> parse() {
        InputStream inputStream = new ByteArrayInputStream(this.sourceData.getBytes(StandardCharsets.UTF_8));
        InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
        List<T> resList = new CsvToBeanBuilder<T>(inputStreamReader)
                .withType(this.type)
                .withSkipLines(1)
                .build()
                .parse();
        try {
            inputStreamReader.close();
            inputStream.close();
        } catch (IOException e) {
            log.error("## 关闭流异常 ", e);
        }finally {
            try {
                inputStreamReader.close();
                inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return resList;
    }

    /**
     * 解析csv
     *
     * @return list<T>
     */
    public List<T> parse(String billDownloadUrl) throws Exception {
        InputStream inputStream = null;
        URL url = null;
        HttpURLConnection httpUrlConnection = null;
        url = new URL(billDownloadUrl);
        httpUrlConnection = (HttpURLConnection) url.openConnection();
        httpUrlConnection.setConnectTimeout(5 * 1000);
        httpUrlConnection.setDoInput(true);
        httpUrlConnection.setDoOutput(true);
        httpUrlConnection.setUseCaches(false);
        httpUrlConnection.setRequestMethod("GET");
        httpUrlConnection.setRequestProperty("Charsert", "UTF-8");
        httpUrlConnection.connect();
        inputStream = httpUrlConnection.getInputStream();
        InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
        List<T> resList = new CsvToBeanBuilder<T>(inputStreamReader)
                .withType(this.type)
                .withSkipLines(1)
                .build()
                .parse();
        try {
            inputStreamReader.close();
            inputStream.close();
        } catch (IOException e) {
            log.error("## 关闭流异常 ", e);
        }finally {
            try {
                inputStreamReader.close();
                inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return resList;
    }

    /**
     * 转换的Javabean class
     *
     * @param type javabean.class
     * @return
     */
    public HilifeCsvToBeanBuilder<T> withType(Class<? extends T> type) {
        this.type = type;
        return this;
    }

    /**
     * 要解析的支付三方类型 微信  支付宝
     *
     * @param payType
     * @return
     */
    public HilifeCsvToBeanBuilder<T> withPayType(String payType) {
        this.payType = payType;
        return this;
    }

}
